package com.huaxiafinance.management.controller;


import com.huaxiafinance.management.dto.request.QueryPackageDto;
import com.huaxiafinance.management.dto.request.UpdatePackageDto;
import com.huaxiafinance.management.dto.request.UpdatePackageSortDto;
import com.huaxiafinance.management.service.PartnerPackageService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 签到记录管理控制器
 */
@Controller
@RequestMapping("/package")
public class PartnerPackageController {

  private static final Logger logger = Logger.getLogger(PartnerPackageController.class);

  @SuppressWarnings("SpringJavaAutowiringInspection")
  @Autowired
  private PartnerPackageService partnerPackageService;

  /**
   * 按指定条件查询签到记录
   */
  @RequestMapping(value = "/getPackageList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getPackageList(@Valid @ModelAttribute("dto") QueryPackageDto dto) {
    logger.info("【查询合伙人套餐列表请求参数】=================================>" + dto);
    MessageResult result = partnerPackageService.getPackageList(dto);
    logger.info("【查询合伙人套餐列表响应数据】=================================>" + result.getAsJSON());
    return result.getAsJSON();
  }

  @RequestMapping(value = "/updatePackage", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addPackage(@Valid @RequestBody UpdatePackageDto dto, BindingResult bindingResult) {
    logger.info("【添加或修改合伙人套餐请求参数】=================================>" + dto);
    MessageResult result = new MessageResult();
    if (bindingResult.hasErrors()) {
      result.setFailureResult(bindingResult);
    } else {
      result = partnerPackageService.updatePackage(dto);
    }
    logger.info("【添加或修改合伙人套餐表响应数据】=================================>" + result.getAsJSON());
    return result.getAsJSON();
  }

  @RequestMapping(value = "/getPackageDetail", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getPackageDetail(Integer id) {
    logger.info("【查询合伙人套餐详情请求参数】=================================>" + "id=" + id);
    MessageResult result = partnerPackageService.getPackageDetail(id);
    logger.info("【查询合伙人套餐列表响应数据】=================================>" + result.getAsJSON());
    return result.getAsJSON();
  }

  @RequestMapping(value = "/updateStatus", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateStatus(Integer id, Integer isEnable, Integer adminId) {
    logger.info("【编辑合伙人套启用状态请求参数】=================================>" + "id=" + id);
    MessageResult result = partnerPackageService.updateStatus(id, isEnable, adminId);
    logger.info("【编辑合伙人套启用状态响应数据】=================================>" + result.getAsJSON());
    return result.getAsJSON();
  }

  @RequestMapping(value = "/updateSort", produces = "application/json; charset=utf-8", method = RequestMethod.POST)
  public @ResponseBody
  String updateSort(@Valid @RequestBody UpdatePackageSortDto dto, BindingResult bindingResult)
      throws Exception {
    logger.info("【调整合伙人顺序请求参数】=================================>" + "dto=" + dto);
    MessageResult result = new MessageResult();
    boolean flag;
    if (bindingResult.hasErrors()) {
      result.setFailureResult(bindingResult);
      return result.getAsJSON();
    } else {
      flag = partnerPackageService.changeSort(dto);
    }
    if (flag) {
      result.setSuccessResult();
    } else {
      result.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }
    logger.info("【调整合伙人套餐顺序请求参数】=================================" + result.getAsJSON());
    return result.getAsJSON();
  }

}
